package com.qixin.web.controller.Car;


import com.alibaba.fastjson.JSONObject;
import com.qixin.api.bean.ComCar;
import com.qixin.api.bean.ComFile;
import com.qixin.api.bean.ComIdcardFile;
import com.qixin.api.bean.FileResult;
import com.qixin.api.service.ComCarService;
import com.qixin.api.service.ComFileService;
import com.qixin.api.service.ComIdcardFileService;
import com.qixin.util.file.upload.UploadFile;
import com.qixin.web.util.SessionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 租车，车辆信息 前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2018-10-02
 */
@Controller
@RequestMapping("/WechatCar")
public class ComCarController {
    private static final Logger logger = LoggerFactory.getLogger(ComCarController.class);
	@Autowired
    ComCarService comCarService;
     @Autowired
    ComFileService comFileService;

    /**
     * 跳转组车首页
     * @return
     */
    @RequestMapping("/index.html")
    public String index( ) {
        return   "/Car/CarIndex";
    }

    /**
     * 跳转车源加盟
     * @return
     */
    @RequestMapping("/CarAdd.html")
    public String CarAdd( ) {
        return   "/Car/CarAdd";
    }
    /**
     * 跳转组车详情
     * @return
     */
    @RequestMapping("/info.html")
    public String index(Long id , Model model) {
        model.addAttribute("carId",id);
        return   "/Car/CarInfo";
    }

    /**
     * 关联 动态条件查询已经通过审核的车辆概览信息 带首页图片URl地址
     * 如果url为空 说明暂未上传图片
     */
    @RequestMapping("/findCar")
    @ResponseBody
    public List<ComCar> findCar(String param) {
//        System.out.println(carSize);
//        System.out.println(carKm);
//        System.out.println(base);
//        System.out.println(carBrand);
        List<ComCar> comCars= comCarService.findCarByParam(param);
        logger.info("本次查询出的所有车辆概览"+comCars.toString());
        return   comCars;
    }
    @RequestMapping("/findCara")
    @ResponseBody
    public List<ComCar> findCara(String param,String carSize,String carKm,String base,String carBrand) {
//        System.out.println(carSize);
//        System.out.println(carKm);
//        System.out.println(base);
//        System.out.println(carBrand);
        List<ComCar> comCars= comCarService.findCarByParama(param,carSize,carKm,base,carBrand);
        logger.info("本次查询出的所有车辆概览"+comCars.toString());
        return   comCars;
    }

    /**
     *
     * @param carId 需要传的车辆ID 用于查询本车辆的详细信息
     * @return
     */
    @RequestMapping("/findCarByCarId")
    @ResponseBody
    public  ComCar  findCarByCarId(Long carId,String preUrl,HttpSession session) {
        System.out.println(preUrl);
        session.setAttribute("preUrl",preUrl);
       ComCar  comCar = comCarService.findCarByCarId(carId);
        logger.info("本次查询出的所有车辆详细信息"+comCar.toString());
        return   comCar;
    }
    /**
     *
     * @param carId 需要传的车辆ID 用于查询本车辆的轮播图
     * @return
     */
    @RequestMapping("/findCarImagesByCarId")
    @ResponseBody
    public  List<ComFile>  findCarImagesByCarId(Long carId) {
        Map map=new HashMap<>();
        map.put("relation_id",carId);
        map.put("is_car_index",2);
      List<ComFile> comFiles=  comFileService.selectByMap(map);
        logger.info("本次查询出的所有车辆轮播图"+comFiles.toString());
        return   comFiles;
    }






    /**
     * 文件上传
     * @return
     */
    @RequestMapping("/upload")
    @ResponseBody
    public FileResult upload(@RequestParam("file") MultipartFile file, @RequestParam("isCarIndex") Integer isCarIndex) {
        ComFile comFile=new ComFile();
        comFile.setIsCarIndex(isCarIndex);
        return  comFileService.upload(file,comFile);
    }

    /**
     * 车源加盟添加
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public String insert(String param,@RequestParam("filesId[]")Long[] filesId,Long resultURL,HttpSession session) {
        logger.info(param);
        ComCar comCar=JSONObject.parseObject(param,ComCar.class);
        comCar.setUpcarTime(new Date().getTime());
        comCar.setIsShow(2);
        comCar.setGrounding(2);
        comCar.setFlag(SessionUtil.getUserFlag(session));
        comCar.setUserid(SessionUtil.getUserId(session));
        try {
            return    comCarService.insertcar(comCar, filesId,resultURL);
        } catch (SQLException e) {
            e.printStackTrace();
            return "保存出现意外，有图片丢失。";
        }

    }




    /**
     * 跳转组车成功首页
     * @return
     */
    @RequestMapping("/success.html")
    public String success( ) {
        return   "/success";
    }


}
